<?php

	class Fx_Data_Row_Helper
	{
		protected static $_instance = null;
		const SORT_ASC  = 'ASC';
		const SORT_DESC = 'DESC';
		
		public static function getInstance()
		{
			if (null === self::$_instance)		
			{
				self::$_instance = new self();	
			}
			return self::$_instance;
		}
		
		public function sortByField(& $collection, $field, $sorting = 'ASC')
		{ 
			$sorting = strtoupper($sorting);
			if (count($collection) == 0) 
			{
				return $this;	
			} 
			$startFunc = 'if ($entity1 === null || $entity2 === null || !$entity1->isLoaded() || !$entity2->isLoaded())
					{
						return 0;	
					}
					$method = "get" . ucfirst("'.$field.'");
					$value1 = $entity1->$method();
					$value2 = $entity2->$method(); 
					';
			if ($sorting==self::SORT_ASC) 
			{
				$endFunc = 'return (strnatcasecmp($value1, $value2));';	
			} 
			else if ($sorting==self::SORT_DESC)
			{
				$endFunc = 'return (strnatcasecmp($value2, $value1));';
			}
			$fn = create_function('$entity1, $entity2', $startFunc.$endFunc);
			uasort($collection, $fn);
			return $this;
		}
		
		public function sortRowCollection(& $collection, $field, $sorting = 'ASC')
		{ 
			$sorting = strtoupper($sorting);
			if (count($collection) == 0) 
			{
				return $this;	
			} 
			$startFunc = '
					$entity1 = $row1->getModel();
					$entity2 = $row2->getModel();
					if ($entity1 === null || $entity2 === null || !$entity1->isLoaded() || !$entity2->isLoaded())
					{
						return 0;	
					}
					$method = "get" . ucfirst("'.$field.'");
					$value1 = $entity1->$method();
					$value2 = $entity2->$method(); 
					';
			if ($sorting==self::SORT_ASC) 
			{
				$endFunc = 'return (strnatcasecmp($value1, $value2));';	
			} 
			else if ($sorting==self::SORT_DESC)
			{
				$endFunc = 'return (strnatcasecmp($value2, $value1));';
			}
			$fn = create_function('$row1, $row2', $startFunc.$endFunc);
			uasort($collection, $fn);
			return $this;
		}
		
		public function filterByField(&$collection, $field, $operator, $value)
		{
			$operator = strtolower($operator);
			$fn = null;
			$startFunc = '$method = "get" . "'.ucfirst($field).'";
						  $fieldValue = $model->$method(); ';
			$endFunc = ' return ($fieldValue == "'.$value.'");';
			switch ($operator)
			{
				case '=':
				case 'equal':
					$endFunc = ' return ($fieldValue == "'.$value.'");';
				break;
				case 'like':
					$value = str_replace('%', '[.]*', $value);
					$endFunc = 'return preg_match("/'.$value.'/", $fieldValue);';
				break;
				case '>=':
				case '>':
				case '<=':
				case '!=':
				case '<':
					$endFunc = 'return ($fieldValue '.$operator.' "'.$value.'"); ';
				default:
				break;
			}
			$fn = create_function('$model', $startFunc . $endFunc);
			$collection = & array_filter($collection, $fn);
			return $this;
		}
		
		public function filterRowCollection(&$collection, $field, $operator, $value)
		{
			$operator = strtolower($operator);
			$fn = null;
			$startFunc = '$model = $row->getModel();
						  $method = "get" . "'.ucfirst($field).'";
						  $fieldValue = $model->$method(); ';
			$endFunc = ' return ($fieldValue == "'.$value.'");';
			switch ($operator)
			{
				case '=':
				case 'equal':
					$endFunc = ' return ($fieldValue == "'.$value.'");';
				break;
				case 'like':
					$value = str_replace('%', '[.]*', $value);
					$endFunc = 'return preg_match("/'.$value.'/", $fieldValue);';
				break;
				case '>=':
				case '>':
				case '<=':
				case '!=':
				case '<':
					$endFunc = 'return ($fieldValue '.$operator.' "'.$value.'"); ';
				default:
				break;
			}
			$fn = create_function('$row', $startFunc . $endFunc);
			$collection = & array_filter($collection, $fn);
			return $this;
		}
	}
?>